package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.DTResponseDto;
import com.hualude.roadsign.dto.MaintenanceDto;
import com.hualude.roadsign.dto.RoadpoleDto;
import com.hualude.roadsign.repository.*;
import com.hualude.roadsign.service.FaultHandleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by shao-yikun on 2017/4/11.
 */

@Service
public class FaultHandleServiceImpl implements FaultHandleService {

    @Resource
    private MaintenanceRepository maintenanceRepository;

    @Resource
    private RoadSignRepository roadSignRepository;

    @Resource
    private RoadPoleRepository roadPoleRepository;

    @Resource
    private UserRepository userRepository;

    @Override
    public DTResponseDto getFaultHandle4DT(DTRequestDto dtRequestDto,
                                           Integer orgId,
                                           Integer maintenanceStatus,
                                           String actorIds) throws Exception {
        List<MaintenanceDto> maintenanceDtos = maintenanceRepository.getFaultHandle4DT
                (dtRequestDto, orgId, maintenanceStatus, actorIds);
        Integer count = maintenanceRepository.getCount4DT
                (dtRequestDto.getSearch(), orgId, maintenanceStatus, actorIds);
        for(MaintenanceDto maintenanceDto : maintenanceDtos) {
            Integer maintenancePeopleId = maintenanceDto.getMaintenancePeopleId();
            if(maintenancePeopleId != null) {
                maintenanceDto.setUserName(userRepository.getUserNameByUserId(maintenancePeopleId));
            }
        }
        return new DTResponseDto(dtRequestDto.getDraw(), count, count, maintenanceDtos);
    }

    @Override
    public List<MaintenanceDto> getFaultHandle(Integer orgId,
                                               Integer maintenanceStatus,
                                               String actorIds,
                                               Integer mCount,
                                               Integer mySize) throws Exception {
        List<MaintenanceDto> maintenanceDtos = maintenanceRepository.getFaultHandle(
                orgId, maintenanceStatus, actorIds, mCount, mySize);
        for(MaintenanceDto maintenanceDto : maintenanceDtos) {
            Integer maintenancePeopleId = maintenanceDto.getMaintenancePeopleId();
            if(maintenancePeopleId != null) {
                maintenanceDto.setUserName(userRepository.getUserNameByUserId(maintenancePeopleId));
            }
        }
        return maintenanceDtos;
    }

    @Override
    @Transactional
    public Integer addMaintenanceContent(Integer maintenanceId, String maintenanceContent) throws Exception {
        return maintenanceRepository.addMaintenanceContent(maintenanceId, maintenanceContent);
    }

    @Override
    @Transactional
    public Integer updateMaintenance(MaintenanceDto maintenanceDto) throws Exception {
        if(maintenanceDto.getMaintenanceSignStatus() == 0) {
            roadSignRepository.updateRoadSignState(maintenanceDto.getRoadSignId());
            Integer roadPoleId = roadSignRepository.getRoadPoleIdByRoadSignId(maintenanceDto.getRoadSignId());
            if(roadSignRepository.getRoadSignSecuritySumByRoadPoleId(roadPoleId) == 0)
                roadPoleRepository.updateRoadPoleState(roadPoleId);
        }
        return maintenanceRepository.updateMaintenance(maintenanceDto.getMaintenanceId(),
                                                       maintenanceDto.getMaintenanceSignStatus(),
                                                       maintenanceDto.getMaintenanceExplain(),
                                                       maintenanceDto.getMaintenancePic(),
                                                       maintenanceDto.getMaintenancePeopleId(),
                                                       maintenanceDto.getMaintenanceTime());
    }

    @Transactional
    public MaintenanceDto getMaintenanceByMaintenanceId(Integer maintenanceId) throws Exception {
        return maintenanceRepository.getMaintenanceByMaintenanceId(maintenanceId);
    }

    public MaintenanceDto getReceiptByMaintenanceId(Integer maintenanceId) throws Exception {
        return maintenanceRepository.getReceiptByMaintenanceId(maintenanceId);
    }

}
